Developer Documentation
PATHJava Developer Documentation > Mac OS Runtime for Java > JManager > Programming With JManager


Keyboard Events

Keyboard events occur whenever the user presses a key. These keypresses may correspond to text entered into a window, a keyboard-equivalent menu selection, or a similar action (for example, selecting the default button in a dialog box by pressing the return key). If the keyboard event occurs in a window that corresponds to a frame, you must pass the event to the frame using either the JMFrameKey function JMFrameKey for key-down events or the JMFrameKeyRelease function JMFrameKeyRelease for key-up events.

Listing 1-17 shows a simple example that handles a keyboard event.

Listing 1-17 Handling a keyboard event

static void handleKey(const EventRecord* eve)
{
    WindowPtr win;
    JMFrameRef frame;
    
    /* see if a menu item was selected */
    if (eve->what == keyDown && (eve->modifiers & cmdKey) == cmdKey) {
        long menuResult = MenuEvent(eve); /*assumes Appearance Manager*/
        if (menuResult != 0) {
            menuHit(menuResult >> 16, menuResult & 0xffff);
            return;
        }
    }
    /* otherwise, just let JManager deal with it */
    win = FrontWindow();
    if (win) {
        frame = (JMFrameRef) GetWRefCon(win);
        if (frame)
            if (eve->what ==keyUp)
                JMFrameKeyRelease(frame, eve->message & charCodeMask,
                    (eve->message & keyCodeMask) >> 8, eve->modifiers);
                else JMFrameKey(frame, eve->message & charCodeMask,
                    (eve->message & keyCodeMask) >> 8, eve->modifiers);
    }
}

This code first checks to see if the keyboard input was a keyboard equivalent for a menu item (for example, Command-Q for Quit). If so, control passes to the menu-event routine (see Listing 1-18 for an example of handling a menu selection). In all other cases, the keyboard input is passed to the frame corresponding to the window, and the Java program can then determine the appropriate response. The content of the keyboard input is determined from the event record (the EventRecord structure) returned by the Event Manager.


© 1998 Apple Computer, Inc. — (Last Updated 3 Dec 98)